<?php
function ajaxReturn($array){
    $content=json_encode($array);
    if(empty($_GET['callback'])){
        echo $content;exit;
    }else{
        echo $_GET['callback']."(".$content.")";exit;
    }
}
/**
 * 缓存
 * @param $key 缓存名
 * @param $value 缓存值 为空时为 读,不为空为写
 */
function filecache($key,$value=''){
    if(is_array($key))$key=md5(json_encode($key));
    $filename='cache/'.$key.'.c';clearcache();
    if(empty($value)){
        $res=@file_get_contents($filename);return json_decode($res,true);
    }else{
        if(!is_array($value))$value=array($value);
        $value=json_encode($value);
        file_put_contents($filename,$value);
    }return $filename;
}
//清空所有缓存
function clearcache(){
   $cachepath='cache/';$cachename='cachetime.cache';
   $cachetime=@file_get_contents($cachepath.$cachename);
   $date=date('Y-m-d');
   if($cachetime==$date)return false;
   foreach(scandir($cachepath) as $fn) {
       if(in_array($fn,array(".","..",$cachename)))continue;
	   unlink($cachepath.$fn);
   }file_put_contents($cachepath.$cachename,$date);
   return true;
}
?>